package com.ayou.eggguard.user.controller;


import com.ayou.common.utils.*;
import com.ayou.eggguard.user.pojo.User;
import com.ayou.eggguard.user.service.UserService;
//import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public Result addUser(@Pattern(regexp = "^\\S{5,16}$") String username,
                          @Pattern(regexp = "^\\S{5,16}$")String password){
        if(userService.getUserById(username)==null){
            userService.addUser(username,password);
            return Result.success("注册成功");
        }else{
            return Result.error("用户名已存在");
        }
    }

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{5,16}$") String username,
                        @Pattern(regexp = "^\\S{5,16}$")String password){
        User loginUser=userService.getUserById(username);
        if(loginUser!=null&&Md5Util.getMD5String(password).equals(loginUser.getPassword())&& loginUser.getOpen()){
            //登录成功b
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
            claims.put("role",loginUser.getUserRole());
            String token= JwtUtil.genToken(claims);

            return Result.success(token);
        }else{
            return Result.error("用户名或密码错误");
        }
    }

    @GetMapping("/userInfo")
    public Result<User> getUserInfo(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String username=(String)map.get("username");
//        Map<String,Object> map=JwtUtil.parseToken(token);
//        String username=(String)map.get("username");
        User user=userService.getUserByUserName(username);
        return Result.success(user);
    }

    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody User user){
        userService.updateUserInfo(user);
        return Result.success();
    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> params){
        //1、校验参数
        String oldPwd=params.get("old_pwd");
        String newPwd=params.get("new_pwd");
        String rePwd=params.get("re_pwd");
        if(!StringUtils.hasLength(oldPwd)||!StringUtils.hasLength(newPwd)||!StringUtils.hasLength(rePwd)){
            Result.error("缺少必要的参数");
        }
        if(!(newPwd.length()>=5&&newPwd.length()<=15)){
            return Result.error("密码应为5到15个字符");
        }
        //校验原密码是否正确
        //根据用户名校验原密码是否和oldPwd相同
        Map<String,Object> map=ThreadLocalUtil.get();
        String username=(String) map.get("username");
        User loginUser=userService.getUserByUserName(username);
        if(!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("原密码错误");
        }
        //newPwd和rePwd是否一样
        if(!rePwd.equals(newPwd)){
            return Result.error("两次填写的密码不一致");
        }
        //2、调用service完成密码更新
        userService.updatePwd(username,newPwd);
        return Result.success();
    }

    @GetMapping("/getUserListByPage")
    public Result<PageBean<User>> getUserListByPage(Integer pageNum,
                                                    Integer pageSize,
                                                    @RequestParam(required = false) String username){
        PageBean<User> pb=userService.getUserListByPage(pageNum,pageSize,username);
        return Result.success(pb);
    }

    @GetMapping("/userOpen")
    public Result userOpen(Integer id){
        userService.userOpen(id);
        return Result.success();
    }
}
